#!/bin/bash

set -e
set -v 

if [ -z "${REGION}" ]; then
  REGION="cn-huhehaote"
fi

echo "REGION: $REGION"

export fc_component_function_name=node16-cli-$(uname)-$(uname -m)-$RANDSTR
functionName=fc3-command-$fc_component_function_name
s remove -y

echo "test command instance/version/alias/concurrency/provision ..."
s deploy -y
instanceId=`s invoke  | grep "Invoke instanceId:" |  sed "s/.*: //"`

# 故意跳转到其他目录
cd ../

s cli fc3@dev instance list --region $REGION --function-name $functionName  -a quanxi

s cli fc3@dev instance exec --instance-id $instanceId --cmd 'ls -lh' --region $REGION --function-name $functionName  -a quanxi
sleep 1

s cli fc3@dev version publish --description "test version" --region $REGION --function-name $functionName  -a quanxi
s cli fc3@dev version list --region $REGION --function-name $functionName  -a quanxi
s cli fc3@dev version remove --version-id latest  -y --region $REGION --function-name $functionName  -a quanxi
s cli fc3@dev version publish --description "test version2" --region $REGION --function-name $functionName  -a quanxi

s cli fc3@dev concurrency put --reserved-concurrency 80 --region $REGION --function-name $functionName  -a quanxi
s cli fc3@dev concurrency get --region $REGION --function-name $functionName  -a quanxi
s cli fc3@dev concurrency remove -y --region $REGION --function-name $functionName  -a quanxi

s cli fc3@dev alias list --region $REGION --function-name $functionName  -a quanxi
s cli fc3@dev alias publish --alias-name test --version-id latest --region $REGION --function-name $functionName  -a quanxi
s cli fc3@dev alias get --alias-name test --region $REGION --function-name $functionName  -a quanxi
s cli fc3@dev alias list --region $REGION --function-name $functionName  -a quanxi
s cli fc3@dev alias list --table --region $REGION --function-name $functionName  -a quanxi
s cli fc3@dev alias remove --alias-name test  -y --region $REGION --function-name $functionName  -a quanxi
s cli fc3@dev alias publish --alias-name test --version-id latest --region $REGION --function-name $functionName  -a quanxi

s cli fc3@dev provision put --qualifier test --ac --target 2 --scheduled-actions '[{"name":"scheduled-actions","startTime":"2023-08-15T02:04:00.000Z","endTime":"2033-08-15T02:04:00.000Z","target":1,"scheduleExpression":"cron(0 0 4 * * *)"}]' --target-tracking-policies '[{"name":"target-tracking-policies","startTime":"2023-08-15T02:05:00.000Z","endTime":"2033-08-15T02:05:00.000Z","metricType":"ProvisionedConcurrencyUtilization","metricTarget":0.6,"minCapacity":1,"maxCapacity":3}]' --region $REGION --function-name $functionName  -a quanxi
s cli fc3@dev provision get --qualifier test --region $REGION --function-name $functionName  -a quanxi
s cli fc3@dev provision list --region $REGION --function-name $functionName  -a quanxi
s cli fc3@dev provision remove --qualifier test -y --region $REGION --function-name $functionName  -a quanxi
s cli fc3@dev provision list --region $REGION --function-name $functionName  -a quanxi
s cli fc3@dev provision put --qualifier test --target 2  --region $REGION --function-name $functionName  -a quanxi

s cli fc3@dev remove -y  --region $REGION --function-name $functionName  -a quanxi


cd ./command-api
export layer_name=pyyaml-layer-$(uname)-$(uname -m)-$RANDSTR
s cli fc3@dev layer list --prefix Python --region $REGION -a quanxi
s cli fc3@dev layer info --layer-name Python39-Scrapy2x --version-id 2 --region $REGION -a quanxi 
s cli fc3@dev layer download --layer-name Python39-Scrapy2x --version-id 2 --region $REGION -a quanxi
s cli fc3@dev layer publish --layer-name $layer_name --code ./pyyaml-layer.zip --compatible-runtime "python3.9,python3.10,custom,custom.debian10" --region $REGION -a quanxi --debug
s cli fc3@dev layer list --prefix $layer_name --region $REGION -a quanxi
s cli fc3@dev layer list --prefix $layer_name --table --region $REGION -a quanxi
s cli fc3@dev layer versions --layer-name $layer_name --region $REGION -a quanxi
s cli fc3@dev layer remove -y --layer-name $layer_name --region $REGION -a quanxi